package com.laikan.reader.core.view;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.laikan.reader.R;
import com.socks.library.KLog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MultipleTextView extends RelativeLayout {

    Resources resources = getResources();
    private Context context;
    private float textSize;
    private int textColor;
    private int wordMargin;
    private int lineMargin;
    private int textPaddingLeft;
    private int textPaddingRight;
    private int textPaddingTop;
    private int textPaddingBottom;
    private Drawable textBackground;
    private int ColorInt[] = {resources.getColor(R.color.multiple_1), resources.getColor(R.color.C_A9DFF5), resources.getColor(R.color.C_FFA587), resources.getColor(R.color.C_F3C4E3), resources.getColor(R.color.C_FFA587), resources.getColor(R.color.C_FFB128)};
    private int layout_width;

    private OnMultipleTVItemClickListener listener;

    public MultipleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context = context;
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);
        textColor = array.getColor(R.styleable.MyView_textColors, 0XFF00FF00); // 提供默认值，放置未指定
        textSize = array.getDimension(R.styleable.MyView_textSizes, 24);
        textSize = px2sp(context, textSize);
        wordMargin = array.getDimensionPixelSize(R.styleable.MyView_textWordMargin, 0);
        lineMargin = array.getDimensionPixelSize(R.styleable.MyView_textLineMargin, 0);
        textBackground = array.getDrawable(R.styleable.MyView_textBackground);
        textPaddingLeft = array.getDimensionPixelSize(R.styleable.MyView_textPaddingLeft, 0);
        textPaddingRight = array.getDimensionPixelSize(R.styleable.MyView_textPaddingRight, 0);
        textPaddingTop = array.getDimensionPixelSize(R.styleable.MyView_textPaddingTop, 0);
        textPaddingBottom = array.getDimensionPixelSize(R.styleable.MyView_textPaddingBottom, 0);
        array.recycle();
        // 下边是获取系统属性
        int[] attrsArray = new int[]{android.R.attr.id, // 0
            android.R.attr.background, // 1
            android.R.attr.layout_width, // 2
            android.R.attr.layout_height, // 3
            android.R.attr.layout_marginLeft, // 4
            // 这个地方必须放到android.R.attr.layout_width的下边
            // 不知道为什么
            android.R.attr.layout_marginRight // 5
        };
        TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray);

        try {
            layout_width = ta.getDimensionPixelSize(2, ViewGroup.LayoutParams.MATCH_PARENT);
        }catch (Exception e) {
            // TODO: handle exception
            DisplayMetrics dm = getResources().getDisplayMetrics();
            layout_width = dm.widthPixels;
        }
        int marginRight = ta.getDimensionPixelSize(4, 0);
        int marginLeft = ta.getDimensionPixelSize(5, 0);
        layout_width = layout_width - marginRight - marginLeft;

        KLog.e("aa", "ddd_" + textSize);

        ta.recycle();

    }

    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int)(pxValue / fontScale + 0.5f);
    }

    public OnMultipleTVItemClickListener getOnMultipleTVItemClickListener() {
        return listener;
    }

    public void setOnMultipleTVItemClickListener(OnMultipleTVItemClickListener listener) {
        this.listener = listener;
    }

    public void setTextViews(List<String> dataList) {
        if (dataList == null || dataList.size() == 0) {
            return;
        }

        // 每一行拉伸
        int line = 0;
        Map<Integer, List<TextView>> lineMap = new HashMap<Integer, List<TextView>>();
        List<TextView> lineList = new ArrayList<TextView>();
        lineMap.put(0, lineList);

        int x = 0;
        int y = 0;

        for (int i = 0; i < dataList.size(); i++) {
            TextView tv = new TextView(context);
            tv.setText(dataList.get(i));
            tv.setTextSize(textSize);
            if (i % 6 == 0) {
                tv.setBackgroundResource(R.drawable.search_multip_background1);
            }else if (i % 6 == 1) {
                tv.setBackgroundResource(R.drawable.search_multip_background2);
//                tv.setBackgroundColor(ColorInt[1]);
            }else if (i % 6 == 2) {
                tv.setBackgroundResource(R.drawable.search_multip_background3);
            }else if (i % 6 == 3) {
                tv.setBackgroundResource(R.drawable.search_multip_background4);
            }else if (i % 6 == 4) {
                tv.setBackgroundResource(R.drawable.search_multip_background5);
            }else if (i % 6 == 5) {
                tv.setBackgroundResource(R.drawable.search_multip_background6);
            }
            //			if (textBackground != null)
            //				tv.setBackgroundDrawable(textBackground);

            tv.setTextColor(textColor);
            tv.setPadding(textPaddingLeft, textPaddingTop, textPaddingRight, textPaddingBottom);
            tv.setTag(i);// 标记position
            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (listener != null) {
                        listener.onMultipleTVItemClick(v, (Integer)v.getTag());
                    }
                }
            });

            int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            tv.measure(w, h);
            int tvh = tv.getMeasuredHeight();
            int tvw = getMeasuredWidth(tv);

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            if (x + tvw > layout_width) {
                x = 0;
                y = y + tvh + lineMargin;

                // 拉伸处理
                line++;
                lineMap.put(line, new ArrayList<TextView>());
            }
            lp.leftMargin = x;
            lp.topMargin = y;
            x = x + tvw + wordMargin;
            tv.setLayoutParams(lp);

            // 拉伸处理
            lineMap.get(line).add(tv);

        }
        // 每一行拉伸
        for (int i = 0; i <= line; i++) {
            // 该行最后一个位置
            int len = lineMap.get(i).size();
            if (len > 0) {

                TextView tView = lineMap.get(i).get(len - 1);

                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)tView.getLayoutParams();
                // 自动拉伸
                // int right = lp.leftMargin + getMeasuredWidth(tView);
                // int emptyWidth = layout_width - right;
                // 自动换行
                int emptyWidth = lp.width;
                int padding = emptyWidth / (len * 2);
                int leftOffset = 0;
                for (int j = 0; j < lineMap.get(i).size(); j++) {

                    TextView tView2 = lineMap.get(i).get(j);
                    RelativeLayout.LayoutParams lp2 = (RelativeLayout.LayoutParams)tView2.getLayoutParams();
                    lp2.leftMargin = lp2.leftMargin + leftOffset;
                    leftOffset = (j + 1) * 2 * padding;

                    tView2.setPadding(tView2.getPaddingLeft() + padding, tView2.getPaddingTop(), tView2.getPaddingRight() + padding, tView2.getPaddingBottom());

                    addView(tView2);

                }
            }
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {

        super.dispatchDraw(canvas);

    }

    public int getMeasuredWidth(View v) {
        // return v.getMeasuredWidth() + v.getPaddingLeft() +
        // v.getPaddingRight();
        return v.getMeasuredWidth();
    }

    public interface OnMultipleTVItemClickListener {

        void onMultipleTVItemClick(View view, int position);
    }
}
